import requests
from lxml import etree 
import time 

data = 0
mian_url = 'https://www.bg60.cc/book/637/1.html'
# 定义伪装请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',

    "Referer": "https://www.bg60.cc/book/637/"
    }
def download_bqg(url):
    global data  # 全局变量
    if data <= 20:  # 限制下载的章节数
        data += 1   # 计数
    else:
        return
    response = requests.get(url=url, headers=headers) # 发送请求
    # 判断请求是否成功
    if response.status_code == 200:
        tree = etree.HTML(response.text) # 解析网页 
    else:
        print('请求失败')
    title = tree.xpath("//h1/text()")[0]    # 获取章节名
    text = "\n".join(tree.xpath("//div[@id='chaptercontent']/text()"))  # 获取内容 用join拼接  用\n换行
    print(f"正在下载{title}") 
    next_url = tree.xpath("//a[text()='下一章']/@href")[0]
    with open(f"神话版三国.txt", "a", encoding="utf-8") as f: # 保存到本地
        f.write(title + "\n")
        f.write(text + "\n")
    print(f"{title}下载完成")
    time.sleep(1)  # 休眠1秒
    download_bqg("https://www.bg60.cc" + next_url)  # 递归调用 下载下一章 
download_bqg(mian_url)  # 调用函数


